package com.example.dangdangbackend.controller;

import com.example.dangdangbackend.VO.BookCartVO;
import com.example.dangdangbackend.common.BaseContext;
import com.example.dangdangbackend.entity.Item;
import com.example.dangdangbackend.entity.Result;
import com.example.dangdangbackend.service.ItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 灰天
 */
@RestController
@RequestMapping("/item")
@Slf4j
public class ItemController {
    @Autowired
    private ItemService itemService;

    @PostMapping("/insertIntoItem")
    public Result<String> insertIntoItem(int bookId) {
        int userId = BaseContext.getCurrentId();
        Item item = new Item();
        item.setUserId(userId);
        item.setBookId(bookId);
        boolean b = itemService.checkItem(bookId);
        if (b) {
            return Result.success("自动增加当前图书数目");
        }
        itemService.insertIntoItem(item);
        return Result.success("加入购物车成功");
    }

    @PostMapping("/addItem")
    public Result addItem(int itemId,int bookId) {
        itemService.addItem(itemId,bookId);
        return Result.success();
    }

    @PostMapping("/subItem")
    public Result subItem(int itemId) {
        itemService.subItem(itemId);
        return Result.success();
    }

    @PostMapping("/getItemDetail")
    public Result<List<BookCartVO>> getItemDetail(){
        int userId = BaseContext.getCurrentId();
        List<BookCartVO> itemDetails = itemService.getItemDetail(userId);
        return Result.success(itemDetails);
    }

    @PostMapping("/deleteItem")
    public Result deleteItem(int itemId){
        itemService.deleteItem(itemId);

        return Result.success();
    }
}
